﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AuthoSYS.EF;

namespace AuthoSYS.Web.ViewModels.MappingToModel
{
    public static class RoleMapping
    {
        private static void BaseToModel(Role model, RoleViewModel vm)
        {
            model.Id = vm.Id;
            model.IsLocked = vm.IsLocked;
            model.CreatedOn = vm.CreatedOn;
            model.CreatedBy = vm.CreatedBy;
            model.ModifiedBy = vm.ModifiedBy;
            model.ModifiedOn = vm.ModifiedOn;

            model.Permissions = vm.Permissions;          
            model.Description = vm.Description;
            model.RoleName = vm.RoleName;

            if (vm.Groups == null || vm.Groups.Count() == 0)
            {
                model.Groups.Add(new GroupRole { Id = Guid.NewGuid().ToString(), GroupId = vm.GroupId, RoleId = vm.Id, ModifiedOn = DateTime.Now, CreatedOn = DateTime.Now });
            }
            else
            {
                model.Groups = vm.Groups.Select(g => new GroupRole { Id = g.Id, GroupId = vm.GroupId, RoleId = vm.Id, ModifiedOn = DateTime.Now }) as System.Data.Objects.DataClasses.EntityCollection<GroupRole>;
            }
        }

        public static Role ToModel(this RoleViewModel vm)
        {
            Role model = new Role();

            BaseToModel(model, vm);

            return model;
        }
    }
}